﻿using SimpleCrawler.DAL;
using SimpleCrawler.DependencyInjection;
using SimpleCrawler.Entity;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace SimpleCrawler.Crawler
{
    public class ZhiHuBySouGouCrawler : CrawlerBase
    {    
        //private ILog logger;    
        CrawlerDAL dal;  
        public ZhiHuBySouGouCrawler():base(1006,"搜狗知乎") {
            //logger = LoggerManager.GetLogger(GetType());
            dal = AppServiceProvider.ServiceProvider.GetService<CrawlerDAL>();
        }

        public override void GetStartedPageUrl()
        {
           
            RequestUrl = $" http://zhihu.sogou.com/zhihu?query={System.Web.HttpUtility.UrlEncode(CrawlerTask.KeyWord)}&sourceid=sugg";
        }
        public override void GetNextPageUrl(int page)
        {
            RequestUrl = $" http://zhihu.sogou.com/zhihu?query={System.Web.HttpUtility.UrlEncode(CrawlerTask.KeyWord)}&sourceid=sugg&page={page}";
        }


        public async override Task<int> GetResultContent(string html)
        {
            Match table = Regex.Match(html, @"<div class=""box-result""[\s\S]*?<div class=""box-about""");
            MatchCollection trlist = Regex.Matches(table.ToString(), @"<div class=""result-about-list"">[\s\S]*?</div>[\s]</div>");
            Regex titleReg = new Regex(@"<a target=""_blank""[\s\S]*?>([\s\S]*?)</a>");
            //Regex dateReg = new Regex(@"提问时间:([\s\S]*?)&nbsp;");

            List<Information> listinfo = new List<Information>();
            if (trlist.Count > 0)
            {
                foreach (var tr in trlist)
                {
                    Match Mtitle = titleReg.Match(tr.ToString());
                    //Match Mdate= dateReg.Match(tr.ToString());
                    Information info = new Information();
                    if (Mtitle.Success)
                    {
                        //info.Title = Mtitle.Groups[1].ToString().Trim();
                    }
                    else
                    {
                        continue;
                    }
                    listinfo.Add(info);
                    Console.WriteLine(CleanTag(Mtitle.Groups[1].ToString()).Trim());
                }
            }
            return listinfo.Count;
        }

        protected async Task<bool> SaveToDB(List<Information> list)
        {
            return await dal.AddInfoList(list).ConfigureAwait(continueOnCapturedContext:false) ;
        }
    }
}
